package com.gress.shop.controller.authority;

import com.gress.shop.annotation.FireLogger;
import com.gress.shop.bo.Action;
import com.gress.shop.bo.MemberActionRelation;
import com.gress.shop.service.IActionService;
import com.gress.shop.vo.SwalFactory;
import com.gress.shop.vo.SwalReturn;
import com.wordnik.swagger.annotations.Api;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created xiongwen with 2018/3/7 21:29
 * Description:
 */
@Controller
@RequestMapping("action")
@Api("权限管理")
public class ActionController {

    @Resource
    private IActionService actionService;

    @RequestMapping("to_action")
    @FireLogger("权限管理")
    public String toHtml5(Model model) {
        List<Action> actionList = actionService.queryAllAction();
        model.addAttribute(actionList);
        return "action/to_action";
    }

    @RequestMapping("query_sys_action")
    @FireLogger("查询系统权限列表")
    public String querySysActionHtml5(Model model) {
        List<Action> actions = actionService.queryAllAction();
        model.addAttribute(actions);
        return "action/sys_action";
    }

    @RequestMapping("add_action")
    @ResponseBody
    @FireLogger("新增系统权限")
    public SwalReturn addAction(Action action) {
        actionService.insertAction(action);
        return SwalFactory.getSuccessSwal();
    }

    @RequestMapping("get_all_action")
    @ResponseBody
    @FireLogger("查询系统权限列表")
    public SwalReturn<List<Action>> getAllAction() {
        List<Action> actions = actionService.queryAllAction();
        return SwalFactory.getSuccessSwal().setData(actions);
    }

    @RequestMapping("delete_action")
    @ResponseBody
    @FireLogger("删除系统权限")
    public SwalReturn deleteAction(String id) {
        actionService.deleteByActionId(id);
        return SwalFactory.getSuccessSwal();
    }

    @RequestMapping("update_action")
    @ResponseBody
    @FireLogger("更新系统权限")
    public SwalReturn updateAction(Action action) {
          actionService.updateAction(action);
        return SwalFactory.getSuccessSwal();
    }


    @RequestMapping("query_member_action")
    @FireLogger("用户权限")
    public String queryMemberAction(Model model) {
        model.addAttribute(actionService.queryAllAction());
        model.addAttribute(actionService.queryAllMAR());
        return "action/member_action";
    }

    @RequestMapping("delete_member_action")
    @FireLogger("用户批量删除权限")
    @ResponseBody
    public SwalReturn deleteMemberAction(List<String> relationIdList) {
        actionService.deleteMAR(relationIdList);
        return new SwalReturn();
    }


    @RequestMapping("add_member_action")
    @ResponseBody
    @FireLogger("用户批量授权")
    public SwalReturn addMemberAction(String email, String[] actionId) {
        actionService.insertMemberActionRelation(email, actionId);
        return SwalFactory.getSuccessSwal();
    }

    @RequestMapping("query_member_all_relation")
    @ResponseBody
    @FireLogger("查询所有用户的权限信息")
    public SwalReturn<List<MemberActionRelation>> queryMemberRelation() {
        return SwalFactory.getSuccessSwal().setData(actionService.queryAllMAR());
    }

}
